﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Add_AssMistakeReason : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        public frm_Add_AssMistakeReason()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;
            string reason_code = null;
            string reason_name = null;
            
            if (string.IsNullOrEmpty((this.reasonCode.Text)))
            {
                error = "Please enter Reason Code";
            }
            else
            {
                reason_code = this.reasonCode.Text;
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.reasonName.Text))
                {
                    error = "Please enter Reason Name";
                }
                else
                {
                    reason_name = this.reasonName.Text;
                }
            }
                        
            if (string.IsNullOrEmpty(error))
            {
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [231_MistakeReason] (ReasonCode, ReasonName)"
                        + " VALUES ('" + reason_code + "', N'" + reason_name + "')";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    clearForm();
                    MessageBox.Show("Mistake Reason added success.");

                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Mistake Reason. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
        }
    }
}
